﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Device.Location;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Leuphana.GuidedTour.App.ViewModels
{
    public class HotSpotViewModel : ViewModelBase
    {
        // Fields
        private int _id;
        private string _title;
        private string _description;
        private string _imageSource;
        private bool _isDataLoaded = false;
        private bool _isHighlight = false;
        private GeoCoordinate _location = new GeoCoordinate();
        private ArticleViewModel _selectedArticle = null;

        private HotSpotViewModel _prev = null;
        private HotSpotViewModel _next = null;

        // Constructor
        public HotSpotViewModel()
        {
            Articles = new ObservableCollection<ArticleViewModel>();
            NavigateToCommand = new NavigationCommand("hotspot", "id");
        }

        // Collection properties
        public ObservableCollection<ArticleViewModel> Articles { get; private set; }

        public bool HasSelectedArticle
        {
            get { return _selectedArticle != null; }
        }
        public bool IsHighlight
        {
            get { return _isHighlight; }
            set { if (value != _isHighlight) { _isHighlight = value; OnPropertyChanged("IsHighlight"); } }
        }

        // Data properties
        public int ID
        {
            get { return _id; }
            set { if (value != _id) { _id = value; OnPropertyChanged("ID"); } }
        }
        public string Title
        {
            get { return _title; }
            set { if (value != _title) { _title = value; OnPropertyChanged("Title"); } }
        }
        public string Description
        {
            get { return _description; }
            set { if (value != _description) { _description = value; OnPropertyChanged("Description"); } }
        }
        public string ImageSource
        {
            get { return _imageSource; }
            set { if (value != _imageSource) { _imageSource = value; OnPropertyChanged("ImageSource"); } }
        }

        public GeoCoordinate Location
        {
            get { return _location; }
        }
        public ArticleViewModel SelectedArticle
        {
            get { return _selectedArticle; }
            set
            {
                if (_selectedArticle != value)
                {
                    _selectedArticle = value;

                    OnPropertyChanged("SelectedArticle");
                    OnPropertyChanged("HasSelectedArticle");
                }
            }
        }

        public double Lat
        {
            get { return _location.Latitude; }
            set
            {
                if (value != _location.Latitude)
                {
                    _location.Latitude = value;

                    OnPropertyChanged("Lat");
                    OnPropertyChanged("Location");
                }
            }
        }
        public double Lng
        {
            get { return _location.Longitude; }
            set
            {
                if (value != _location.Longitude)
                {
                    _location.Longitude = value;

                    OnPropertyChanged("Lng");
                    OnPropertyChanged("Location");
                }
            }
        }

        public HotSpotViewModel PrevItem
        {
            get { return _prev; }
            set
            {
                if (value != _prev)
                {
                    _prev = value;
                    OnPropertyChanged("PrevItem");
                    OnPropertyChanged("HasPrev");
                }
            }
        }
        public HotSpotViewModel NextItem
        {
            get { return _next; }
            set
            {
                if (value != _next)
                {
                    _next = value;
                    OnPropertyChanged("NextItem");
                    OnPropertyChanged("HasNext");
                }
            }
        }
        public bool HasNext
        {
            get { return _next != null; }
        }
        public bool HasPrev
        {
            get { return _prev != null; }
        }


        // Commands
        public ICommand NavigateToCommand { get; private set; }

        // Methods
        public void LoadData()
        {
            // Reset selection
            SelectedArticle = null;

            // Only load once
            if (_isDataLoaded)
                return;

            _isDataLoaded = true;

            var formatProvider = new CultureInfo("en-US");

            // Reset collection before refilling
            Articles = new ObservableCollection<ArticleViewModel>();

            // Get Article data
            foreach (var item in TourDataLoader.Current.LoadArticles(ID.ToString()))
            {
                // Create Article
                var viewModel = new ArticleViewModel()
                {
                    ID = Int32.Parse(item.Attribute("ID").Value),
                    Title = item.Element("Title").Value,
                    Description = item.Element("Description").Value,
                    Top = Int32.Parse(item.Element("Top").Value),
                    Left = Int32.Parse(item.Element("Left").Value)
                };

                // AddImages
                foreach (var image in item.Element("Images").Elements("Image"))
                {
                    viewModel.Images.Add("/Images/" + image.Value);
                }

                // AddToListing
                Articles.Add(viewModel);
            }

            // Update properties
            OnPropertyChanged("Articles");
        }
    }
}
